package com.example.a2032.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.entity.JstyleClock;
import com.example.a2032.net.jstylenet.JstyleConst;
import com.example.a2032.net.jstylenet.NetSendUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ClockAdapter extends RecyclerView.Adapter {

    List<JstyleClock> clockList;



    public ClockAdapter(List<JstyleClock> clockList) {
        this.clockList = clockList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ClockViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_clock, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ClockViewHolder settingViewHolder = (ClockViewHolder) holder;
//        settingViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                itemClickListener.onItemClick(position);
//            }
//        });
        JstyleClock clock=clockList.get(position);
        long id=clock.getId();
        int hour=clock.getHour();
        int minute=clock.getMinute();
        boolean enable=clock.isEnable();
        String time= Utils.getFormatTimeString(hour,minute);
        settingViewHolder.tvClockTime.setText(time);
        settingViewHolder.checkboxClock.setChecked(enable);
        settingViewHolder.checkboxClock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                clock.setEnable(isChecked);
                WebSocketUtils.getInstance().sendMsg(NetSendUtils.updateClockState(id+"",isChecked? JstyleConst.OPEN:JstyleConst.CLOSE));
            }
        });
    }

    @Override
    public int getItemCount() {
        return clockList.size();
    }

    static class ClockViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_clockTime)
        TextView tvClockTime;
        @BindView(R.id.checkbox_clock)
        CheckBox checkboxClock;
        public ClockViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    onItemClickListener itemClickListener;

    public void setOnItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface onItemClickListener {
        public void onItemClick(int position);
    }
    public void updateData(List<JstyleClock> clockList){
        this.clockList=clockList;
        notifyDataSetChanged();
    }
    public void clear(){
        this.clockList.clear();
        notifyDataSetChanged();
    }
}
